package io.renren.framework.interceptor;

import com.alibaba.fastjson.JSON;

import com.common.framework.web.response.ResultModel;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * crm权限(Token)验证
 */
@Component
public class AuthorizationInterceptor extends HandlerInterceptorAdapter {

    private static final String USER_KEY = "userId";
    private static final Set<String> NOT_AUTH_URL = new HashSet<String>();


    static {
        NOT_AUTH_URL.add(".*/openapi/.*");
        NOT_AUTH_URL.add(".*/open/.*");
        NOT_AUTH_URL.add(".*/statics/html/.*");
        NOT_AUTH_URL.add(".*/crm/login.htm$");
        NOT_AUTH_URL.add(".*/crm/loginAction$");
        NOT_AUTH_URL.add(".*/app/user/login$");
        NOT_AUTH_URL.add(".*/app/user/registerUser$");
        NOT_AUTH_URL.add(".*/app/user/appWxUserLogIn$");
        NOT_AUTH_URL.add(".*/app/user/xcxUserLogIn$");
        NOT_AUTH_URL.add(".*/app/system/uploadFile$");
        NOT_AUTH_URL.add(".*/app/system/uploadAppPackage$");
        NOT_AUTH_URL.add(".*/app/uploadFile$");
        NOT_AUTH_URL.add(".*/NewFile.jsp$");
        NOT_AUTH_URL.add(".*/swagger-resources$");
        NOT_AUTH_URL.add(".*/*.no$");
        NOT_AUTH_URL.add(".*/*.html$");
        NOT_AUTH_URL.add(".*/*.css$");
        NOT_AUTH_URL.add(".*/*.js$");
        NOT_AUTH_URL.add(".*/*.jsp$");

    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("**********************************************");
        return true;
    }

    /**
     * 判断当前请求是否需要验证回话
     *
     * @param request
     * @return
     */
    private static boolean isAuth(HttpServletRequest request) {
        String reqUrl = request.getRequestURI();

        Iterator<String> notAuthUrlIt = NOT_AUTH_URL.iterator();
        while (notAuthUrlIt.hasNext()) {
            Pattern pattern = Pattern.compile(notAuthUrlIt.next());
            Matcher matcher = pattern.matcher(reqUrl);
            if (matcher.matches()) {
                return false;
            }
        }

        return true;
    }

    /**
     * 构建返回值
     *
     * @param response
     * @param code
     * @param msg
     */
    public static void buildResponse(HttpServletResponse response, Integer code, String msg) {
        response.setStatus(HttpStatus.OK.value());
        response.setHeader("Cache-Control", "no-cache, must-revalidate");
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        try {
            response.getWriter().write(JSON.toJSONString(ResultModel.fail(code, msg)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
